<?php
/**
 * 营销管理接口
 * @author shitou
 *
 */
class CouponAction extends CommonAction {
 
    /**
     * 初始化
     */
    public function _initialize(){
    
    	parent::_initialize();
    	//parent::checkTimestamp();
    }
    
    /**
     * 添加优惠券
     * @access public
     * @param  array  $map     应用参数(company,outletid,addid,addno)
     * @return array
     */
    public function add(){
    	$param = array(
    		"before" => "coupon_add_before",
    		"return" => false
    	);
    	parent::do_add("Coupon",$param);
    }
    
    /**
     * 优惠券详情
     * @access public
     * @param  Intval $id 优惠券id
     * @return array
     */
    public function detail(){
    	
    	$id = $this->_req("id","intval",0);
    	
    	if($id){
    		$data = D("Coupon")->find($id);
    		$this->success($data);
    	}else{
    		$this->error();
    	}
    }
    
    /**
     * 修改优惠券
     * @access public
     * @param  array  $map     应用参数(company,outletid,addid,addno)
     * @return array
     */
    public function edit(){
    	$param = array(
    		"before" => "coupon_add_before",
    		"return" => false
    	);
    	parent::do_edit("Coupon",$param);
    }
    
    /**
     * 删除优惠券
     */
    public function del(){
    	
    	$map['company']  = $this->_req("company","intval",0);
    	$map['outletid'] = $this->_req("outletid","intval",0);
    	
    	$val = $this->_req("val","trim","");
    	if($val){
    		
    		$idArr = explode(',',$val);
    		foreach($idArr as $k=>$v){
    			 
    			$map['id']    = $v;
    			$map['isdel'] = 1;
    			$data = D('Coupon')->save($map);
    		}
    	}
    	 
    	if($data){
    		$this->success($data);
    	}else{
    		$this->error("请选择您要删除的优惠券");
    	}
    }
    
    /**
     * 优惠券列表
     * @access public
     * @param  array  $map    应用参数(company,outletid,addid,addno)
     * @param  string $where  查询条件
     * @return array
     */
    public function index(){
    	$desc      = $this->_get("orderDirection","trim","desc");;
		$order     = $this->_get("orderName","trim","addtime");
		$pageNum   = $this->_get("pageNum","intval",C('PAGE_NUMS'));
		$nowPage   = $this->_get("p","intval",1);
    	$cateid    = $this->_get("cateid","intval",0);
    	$where     = $this->_req("where");
    	$package = $this->_get("package","intval",0);
    	
    	if($where['keywords']){
    		$where['title|keywords'] = array("like","%".$where['keywords']."%");
    		unset($where['keywords']);
    	}
    	
		$model = D("Coupon");
		$count = $model->count();
		$list  = $model->where($where)->order($order.' '.$desc)
		->page($nowPage.','.$pageNum)->select();
		 
		$data  = array("count"=>$count, "list"=>$list);
		$this->success($data);
    }
    
    /**
     * 优惠券列表（无分页）
     * @access public 
     * @param  array  $map 应用参数(company,outletid,addid,addno)
     * @return array
     */
    public function view(){
    	
    	$map['isdel']    = 2;
    	$map['company']  = $this->_req("company","intval",0);
    	$map['outletid'] = $this->_req("outletid","intval",0);
    	
    	$data = D("Coupon")->where($map)->select();
    	$this->success($data);
    }
    
    /**
     * 列出所有的优惠券类型
     */
    public function type(){
    	$list = C('COUPON_TYPE');
    	$this->success($list);
    }
    
    /**
     * 列出所有的优惠券领取场景
     */
    public function sence(){
    	$list = C('COUPON_SCENE');
    	$this->success($list);
    }
    
    /**
     * 用户优惠券列表
     * @access public 
     * @param  array  $map    应用参数(company,outletid,addid,addno)
     * @param  String tableid 用户数据模型
     * @param  String dataid  用户数据id
     * @return array
     */
    public function userView(){
    	
    	$map['company']  = $this->_req("company","intval",0);
    	$map['outletid'] = $this->_req("outletid","intval",0);
    	$map['tableid']  = $this->_req("tableid","trim","");
    	$map['dataid']   = $this->_req("dataid","trim","");
    	
    	$data = M("CouponUser")->where($map)->select();
    	$this->success($data);
    }
    
    /**
     * 用户优惠券使用记录
     * @access public
     * @param  array  $map    应用参数(company,outletid,addid,addno)
     * @param  String tableid 用户数据模型
     * @param  String dataid  用户数据id
     * @return array
     */
    public function userLog(){
    	 
    }
}
?>